local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_cards_station"] or {}
end
local function start_event(inst,player)
    if player.npc_base_lib then
        local ocean_flag = false
        if not TheWorld:HasTag("cave") and math.random(100) < 30 then
            ocean_flag = true
        end
        local pt = player.npc_base_lib:GetRandomPoint(player,player,ocean_flag)
        player.components.playercontroller:Enable(false)
        player.Transform:SetPosition(pt.x,0,pt.z)
        if ocean_flag and TheWorld.Map:IsOceanAtPoint(pt.x,0,pt.z) then
            SpawnPrefab("npc_card_teleportation_mark").Transform:SetPosition(pt.x, 0, pt.z)
            local announce =  string.gsub(GetStringsTable()["npc_card_teleportation.ocean"],"XXXX",player:GetDisplayName(),1)
            player.npc_base_lib:Wisper(announce)
        end

        player.__npc_card_teleportation_task = player:DoPeriodicTask(0.1,function()
            if player.npc_base_lib:Dsitance_T2P(player,pt) < 1 then
                if player.__npc_card_teleportation_task then
                    player.__npc_card_teleportation_task:Cancel()
                    player.__npc_card_teleportation_task = nil
                    player.components.playercontroller:Enable(true)
                end    
            else
                player.Transform:SetPosition(pt.x,0,pt.z)
            end
        end)
    end
    inst:Remove()
end


local function fn_mark()
    local inst = CreateEntity()
    inst.entity:AddTransform()
	inst.entity:AddNetwork()
	inst.entity:AddMiniMapEntity()

	inst.MiniMapEntity:SetIcon("npc_card_teleportation_mark.tex")   --- 地图上的红叉

	inst:AddTag("FX")        
    inst:AddTag("INLIMBO")
    inst:AddTag("NOCLICK")
    inst.entity:SetPristine()    
    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("npc_everything_data")
    
    -- inst:WatchWorldState("cycles", function()
    --     if inst.npc_everything_data:Add("day",1) >100 then
    --         inst:Remove()
    --     end
    -- end)

    inst:AddComponent("playerprox")
    inst.__create_check_task = inst:DoPeriodicTask(1,function()                                         ----- 防止一上来就删除标记
        if inst:IsNearPlayer(20) == false or inst.npc_everything_data:Get("CreateMark") == true then

            inst.components.playerprox:SetDist(10, 13)
            inst.components.playerprox:SetOnPlayerNear(inst.Remove)

            if inst.__create_check_task then
                inst.__create_check_task:Cancel()
                inst.__create_check_task = nil
            end
            inst.npc_everything_data:Set("CreateMark",true)
            -- print("info npc_card_teleportation_mark create")
        end
    end)
    return inst
end

local function fx()
    local inst = CreateEntity()
    inst.entity:AddTransform()

    inst:AddTag("INLIMBO")
    inst:AddTag("FX")

    if not TheWorld.ismastersim then
        return inst
    end

    inst:ListenForEvent("Set",function(inst,player)
        start_event(inst,player)
    end)

    return inst
end

return Prefab("npc_card_teleportation",fx),Prefab("npc_card_teleportation_mark",fn_mark)